import java.util.concurrent.atomic.AtomicBoolean;

public class Solution {
    private static void method1() {
        Object flg = new Object();
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                String str = "1234567";
                try {
                    synchronized (flg) {
                        for (int i = 0; i < str.length(); i++) {
                            System.out.print(str.charAt(i));
                            flg.notify();
                            flg.wait();
                        }
                        flg.notify();
                    }
                } catch (InterruptedException exception) {

                }
            }
        });
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                String str = "abcdefg";
                try {
                    synchronized (flg) {
                        for (int i = 0; i < str.length(); i++) {
                            System.out.print(str.charAt(i));
                            flg.notify();
                            flg.wait();
                        }
                        flg.notify();
                    }
                } catch (InterruptedException exception) {

                }
            }
        });
        thread1.start();
        thread2.start();
    }

    private static void method2() {
        AtomicBoolean flg = new AtomicBoolean(true);
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                String str = "1234567";
                int i = 0;
                while (i < str.length()) {
                    if (flg.get()) {
                        System.out.print(str.charAt(i));
                        flg.set(false);
                        i++;
                    }
                }
            }
        });
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                String str = "abcdefg";
                int i = 0;
                while (i < str.length()) {
                    if (!flg.get()) {
                        System.out.print(str.charAt(i));
                        flg.set(true);
                        i++;
                    }
                }
            }
        });
        thread1.start();
        thread2.start();
    }

    public static void main(String[] args) {
        method1();
    }
}
